package com.blog.service.impl;

import com.blog.mapper.NoticeMapper;
import com.blog.pojo.Notice;
import com.blog.pojo.PageBean;
import com.blog.service.NoticeService;
import com.blog.utils.UserContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    // 分页查看所有公告
    @Override
    public PageBean listAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Notice> noticeList = noticeMapper.listAll();
        Page<Notice> p = (Page<Notice>) (noticeList);
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    // 查看单个公告
    @Override
    public Notice listById(Integer id) {
        Notice notice = noticeMapper.listById(id);
        return notice;
    }

    // 发布公告
    @Override
    public void add(Notice notice) {
        int adminId = Integer.parseInt(UserContext.getUserId());
        notice.setAdminId(adminId);
        notice.setCreated(LocalDateTime.now());
        noticeMapper.insert(notice);
    }

    // 删除公告
    @Override
    public void delete(Integer id) {
        noticeMapper.delete(id);
    }

}
